﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InfoLayer;
using ControlLayer;

namespace GUI
{
    public partial class frmBaoCaoDoanhThu : Form
    {
        private BaoCaoDoanhThuControl baoCaoDoanhThuControl;
        private BaoCaoDoanhThuInfo baoCaoDoanhThuInfo;
        private LoaiPhongControl loaiPhongControl;
        private ChiTietBaoCaoDoanhThuControl chiTietBaoCaoDoanhThuControl;
        private ChiTietBaoCaoDoanhThuInfo chiTietBaoCaoDoanhThuInfo;

        public frmBaoCaoDoanhThu()
        {
            InitializeComponent();
            baoCaoDoanhThuControl = new BaoCaoDoanhThuControl();
            baoCaoDoanhThuInfo = new BaoCaoDoanhThuInfo();
            loaiPhongControl = new LoaiPhongControl();
            chiTietBaoCaoDoanhThuControl = new ChiTietBaoCaoDoanhThuControl();
            chiTietBaoCaoDoanhThuInfo = new ChiTietBaoCaoDoanhThuInfo();
        }

        private void btnLapBaoCao_Click(object sender, EventArgs e)
        {
            if (comboBoxThangLapBaoCao.SelectedItem.ToString() == "")
            {
                MessageBox.Show("Vui lòng nhập tháng báo cáo!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            
         

            int TongDoanhThu = 0;

            //Lay Tong doanh thu
            DataTable dt;
            dt = loaiPhongControl.LayBangDanhSachLoaiPhong();
            int rows = dt.Rows.Count;
            for (int i = 0; i < rows; i++)
            {
                DataTable _dt;
                _dt = baoCaoDoanhThuControl.LayBaoCaoDoanhThu_MaLoaiPhong(dt.Rows[i][0].ToString(), Convert.ToInt32(comboBoxThangLapBaoCao.SelectedItem.ToString()));
                int _rows = _dt.Rows.Count;
                for (int j = 0; j < _rows; j++)
                {
                    TongDoanhThu += Convert.ToInt32(_dt.Rows[j][2].ToString());
                }
            }
            //MessageBox.Show("Tong doanh thu " + TongDoanhThu, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //Them Bang Bao cao doanh thu
            dt = baoCaoDoanhThuControl.LayBangDanhSachBaoCao();
            if (dt.Rows.Count > 0)
                baoCaoDoanhThuInfo.MaBaoCaoDoanhThu = baoCaoDoanhThuControl.TaoMaTuDong(dt.Rows[dt.Rows.Count - 1][0].ToString(), 5);
            else
                baoCaoDoanhThuInfo.MaBaoCaoDoanhThu = baoCaoDoanhThuControl.TaoMaTuDong("MBCDT000", 5);
            baoCaoDoanhThuInfo.ThangBaoCao = Convert.ToInt32(comboBoxThangLapBaoCao.SelectedItem.ToString());

            //Kiem tra xem thang da nhap da duoc bao cao chua?
            if (dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (baoCaoDoanhThuInfo.ThangBaoCao == Convert.ToInt32(dt.Rows[i][1].ToString()))
                    {
                        MessageBox.Show("Tháng " + baoCaoDoanhThuInfo.ThangBaoCao + " đã được báo cáo", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
            else
            {
                //return;
            }

            //Them Bang Chi tiet bao cao doanh thu
            dt = loaiPhongControl.LayBangDanhSachLoaiPhong();
            for (int i = 0; i < rows; i++)
            {
                int doanhThu_LoaiPhong = 0;
                String tenLoaiPhong = "";
                tenLoaiPhong = dt.Rows[i][1].ToString();
                DataTable _dt;
                _dt = baoCaoDoanhThuControl.LayBaoCaoDoanhThu_MaLoaiPhong(dt.Rows[i][0].ToString(), Convert.ToInt32(comboBoxThangLapBaoCao.SelectedItem.ToString()));
                //if (_dt.Rows.Count < 0)
                //{
                //    MessageBox.Show("Thang bao cao chua co trong CSDL.", "Thong Bao", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //    MessageBox.Show("Bao cao khong thanh cong.", "Thong Bao", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //    return;
                //}
                
                for (int j = 0; j < _dt.Rows.Count; j++)
                {
                    doanhThu_LoaiPhong += Convert.ToInt32(_dt.Rows[j][2].ToString());
                    
                }

                DataTable dt_CT;
                dt_CT = chiTietBaoCaoDoanhThuControl.LayBangDanhSachChiTietBaoCao();
                if (dt_CT.Rows.Count > 0)
                    chiTietBaoCaoDoanhThuInfo.MaChiTietBaoCaoDoanhThu = chiTietBaoCaoDoanhThuControl.TaoMaTuDong(dt_CT.Rows[dt_CT.Rows.Count - 1][0].ToString(), 7);
                else
                    chiTietBaoCaoDoanhThuInfo.MaChiTietBaoCaoDoanhThu = chiTietBaoCaoDoanhThuControl.TaoMaTuDong("MCTBCDT000", 7);
                chiTietBaoCaoDoanhThuInfo.MaBaoCaoDoanhThu = baoCaoDoanhThuInfo.MaBaoCaoDoanhThu;
                chiTietBaoCaoDoanhThuInfo.DoanhThu = doanhThu_LoaiPhong;
                if (TongDoanhThu == 0)
                    chiTietBaoCaoDoanhThuInfo.TyLe = 0;
                else
                    chiTietBaoCaoDoanhThuInfo.TyLe = (float)doanhThu_LoaiPhong / TongDoanhThu;
                chiTietBaoCaoDoanhThuInfo.MaLoaiPhong = dt.Rows[i][0].ToString();

                chiTietBaoCaoDoanhThuControl.Insert(ref chiTietBaoCaoDoanhThuInfo);

                //Hien thi len DataGriView
                String[] dong = new string[] { (i + 1).ToString(), tenLoaiPhong, chiTietBaoCaoDoanhThuInfo.DoanhThu.ToString(), (chiTietBaoCaoDoanhThuInfo.TyLe * 100).ToString() + "%" };
                dataGridViewChiTietBaoCao.Rows.Add(dong);
            }

            textBoxTongDoanhThu.Text = TongDoanhThu.ToString();

            baoCaoDoanhThuControl.Insert(ref baoCaoDoanhThuInfo);
            MessageBox.Show("Thêm thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

            //Khong cho nhap Button Lap bao cao 
            btnLapBaoCao.Enabled = false;
        }

        private void buttonThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnInBaoCao_Click(object sender, EventArgs e)
        {
            rptBaoCaoDoanhThu rpt = new rptBaoCaoDoanhThu();

            CrystalDecisions.Shared.TableLogOnInfo logOnInfoTable = new CrystalDecisions.Shared.TableLogOnInfo();
            foreach (CrystalDecisions.CrystalReports.Engine.Table currentTable in rpt.Database.Tables)
            {
                logOnInfoTable.ConnectionInfo.ServerName = Application.StartupPath + "\\QLKS.mdb";
                currentTable.ApplyLogOnInfo(logOnInfoTable);
            }

            frmBaoCaoDoanhThu_CrystalReport frm = new frmBaoCaoDoanhThu_CrystalReport();
            frm.crystalReportViewerBaoCaoDoanhThu.ReportSource = rpt;
            frm.Show();
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            dataGridViewChiTietBaoCao.Rows.Clear();
            textBoxTongDoanhThu.Clear();
            btnLapBaoCao.Enabled = true;
        }
    }
}
